package tq.com.tqstudent.ui.activity.classes;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;

import com.lib.common.base.BaseActivity;
import com.lzy.okgo.OkGo;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;

import butterknife.BindView;
import tq.com.tqstudent.App;
import tq.com.tqstudent.Constants;
import tq.com.tqstudent.R;
import tq.com.tqstudent.adapter.common.CommonAdapter;
import tq.com.tqstudent.adapter.common.MultiItemTypeAdapter;
import tq.com.tqstudent.adapter.common.ViewHolder;
import tq.com.tqstudent.bean.BaseBean;
import tq.com.tqstudent.bean.TopicBean;
import tq.com.tqstudent.bean.UserInfo;
import tq.com.tqstudent.net.ok.DialogCallback;
import tq.com.tqstudent.utils.VarietyUtil;
import tq.com.tqstudent.views.LoginTipsDialog;

/**
 * 题目列表
 *
 * @Author rames
 * @date 2019/3/1 16:22
 */
public class TopicListActivity extends BaseActivity {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.empty_layout)
    LinearLayout emptyLayout;
    private CommonAdapter adapter;
    private int index;
    private int pageNO = 1;
    private String title;
    private int maxCount = 5;

    public static void show(Activity activity, int index) {
        Intent intent = new Intent(activity, TopicListActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt("index", index);
        intent.putExtras(bundle);
        activity.startActivity(intent);
    }

    @Override
    protected void getExtras(Bundle extras) {
        index = extras.getInt("index");
    }

    @Override
    public int initLayout() {
        return R.layout.activity_recyclerview;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        setTitle("题目列表");

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new CommonAdapter<TopicBean>(this, R.layout.item_topic_list, new ArrayList()) {
            @Override
            protected void convert(ViewHolder holder, TopicBean bean, int position) {
                holder.setVisible(R.id.line1, position == 0 ? false : true);
                holder.setInvisible(R.id.line2, position == adapter.getDatas().size()-1 ? false : true);

                holder.setText(R.id.name_tv, bean.teacher.name);
                holder.setText(R.id.project_iv, VarietyUtil.getSubjectName(bean.material.subject));

                holder.setImageResource(R.id.head_bg, VarietyUtil.getCategoryBg(bean.material.category));
                holder.setImageResource(R.id.text_iv, VarietyUtil.getCategoryTextBg(bean.material.category));

                holder.setText(R.id.title_tv, bean.material.title);

                if(!TextUtils.isEmpty(bean.material.content)){
                    holder.setText(R.id.content_tv, bean.material.content);
                } else {
                    String con = "";
                    for (int i = 0; i < bean.material.content_ext.size(); i++) {
                        con += bean.material.content_ext.get(i)+"\n";
                    }
                    holder.setText(R.id.content_tv, con);
                }
            }
        };
        recyclerView.setAdapter(adapter);

        adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                if(!UserInfo.isLogin()){
                    LoginTipsDialog dialog = new LoginTipsDialog(TopicListActivity.this);
                    dialog.show();
                }else {
                    TopicBean bean = (TopicBean) adapter.getDatas().get(position);
                    intentActivity(bean);
                }
            }
        });

        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pageNO++;
                getData();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageNO = 1;
                getData();
            }
        });
    }

    /** 页面跳转处理 */
    private void intentActivity(TopicBean bean){
        int type = bean.material.category;
        int id = bean.material.id;
        switch (type) {
            // 讲评
            case 16:
                EvaluationListActivity.show(this, id);
                break;

            // 提问&群答疑
            case 17:
                QuestionListActivity.show(this, id);
                break;

            // 微课
            case 15:
                MicroListActivity.show(this, id);
                break;

            // 答案&详解
            case 14:
                PdfViewActivity.show(this, id, title);
                break;

            // 分享
            case 18:
                ShareListActivity.show(this, id, maxCount);
                break;

            // 讲义
            default:
                PdfViewActivity.show(this, id, title);
                break;
        }
    }

    @Override
    public void initData() {
        refreshLayout.autoRefresh();
    }

    private void getData() {
        String uri = "api/clz/mlist/"+index+"/"+ Constants.PAGE_SIZE+"/"+pageNO;
        OkGo.<BaseBean<ArrayList<TopicBean>>>get(App.getHttpUrl(uri))
                .tag(this)
                .execute(new DialogCallback<ArrayList<TopicBean>>(TopicListActivity.this) {
                    @Override
                    public void handleSuccess(ArrayList<TopicBean> data) {
                        // 这是加载更多
                        if(refreshLayout.getState() == RefreshState.Loading){
                            adapter.addData(data);
                            refreshLayout.finishLoadMore();
                            if(data.size() < Constants.PAGE_SIZE){
                                refreshLayout.setEnableLoadMore(false);
                            }

                            // 这是下拉刷新
                        } else {
                            if(data.size() > 0) {
                                title = data.get(0).material.title;
                                title = TextUtils.isEmpty(title) ? "题目" : title;
                                setTitle(title);
                                maxCount = data.get(0).material.max_att_cnt;
                            }
                            adapter.getDatas().clear();
                            adapter.setData(data);
                            refreshLayout.finishRefresh();
                            refreshLayout.setEnableLoadMore(true);
                        }

                        emptyLayout.setVisibility(recyclerView.getAdapter().getItemCount() > 0 ? View.GONE : View.VISIBLE);
                    }

                    @Override
                    public void handleFail(BaseBean body) {
                        super.handleFail(body);
                        refreshLayout.finishLoadMore();
                        refreshLayout.finishRefresh();
                    }
                });
    }
}
